package com.cfl.ticket.controller;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cfl.model.City;
import com.cfl.model.Ticket;
import com.cfl.model.TicketNumber;
import com.cfl.model.vo.CityVo;
import com.cfl.model.vo.TicketVo;
import com.cfl.ticket.service.CityService;
import com.cfl.ticket.service.TicketService;
import com.cfl.utils.AppResponse;
import com.cfl.utils.Constant;

@Controller
public class TicketController {

	@Autowired
	private TicketService ticketService;
	@Autowired
	private CityService cityService;
	
	/**
	 * 获取我的车票
	 * @param uid
	 * @param page
	 * @return
	 */
	@RequestMapping("findMyTickets")
	@ResponseBody
	public Object findTickets(String uid, Integer page) {
		
		if(page == null)
		{
			page = 0;
		}
		try {
			if(StringUtils.isBlank(uid))
			{
				return new AppResponse(Constant.code_error, Constant.uid_empty);
			}
			ArrayList<TicketVo> ticketVos = new ArrayList<TicketVo>();
			ArrayList<Ticket> tickets = ticketService.findMyTickets(uid,page);
			for (Ticket ticket : tickets) {
				TicketVo vo = new TicketVo();
				// 出发城市
				City startCity = cityService.findCityByCid(ticket.getStartCity());
				if(startCity != null)
				{
					vo.setStartCity(new CityVo(startCity));
				}
				// 目的城市
				City endCity = cityService.findCityByCid(ticket.getEndCity());
				if(endCity != null)
				{
					vo.setEndCity(new CityVo(endCity));
				}
				// 车次
				TicketNumber number = ticketService.findTicketnumberByTid(ticket.getNumber());
				if(number != null)
				{
					vo.setNumber(number.getNumber());
				}
				vo.setTid(ticket.getTid());	
				vo.setSeat(ticket.getSeat());	// 座位
				vo.setSeatType(ticket.getSeatType());	// 座位类型
				vo.setPrice(ticket.getPrice()+"元");		// 价格
				vo.setSellCity(ticket.getSellCity());	// 售票城市 
				vo.setUid(ticket.getUid());		// 用户uid
				vo.setDriveTimeText(ticket.getDriveTimeText());	// 出发时间
				vo.setBuyName(ticket.getBuyName());	// 乘坐车票人
				ticketVos.add(vo);
			}
			return new AppResponse(ticketVos);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AppResponse(Constant.code_error, Constant.error);
	}
	
	/**
	 * 获取车次信息
	 * @param name
	 * @return
	 */
	@RequestMapping("findTicketnumberInfo")
	@ResponseBody
	public Object getInfo(String name)
	{
		try {
			if(StringUtils.isEmpty(name))
			{
				return new AppResponse("-1", "参数为空");
			}
			TicketVo ticketVo = new TicketVo();
			ArrayList<TicketNumber> ticketNumbers = ticketService.findTicketnumberByAll();
			for (TicketNumber item : ticketNumbers) {
				if(name.contains(item.getNumber()))
				{
					ticketVo.setNumber(item.getNumber());
					String[] strs = name.split(item.getNumber());
					if(strs.length == 2)
					{
						City startCity = cityService.findCityByName(strs[0]);
						City endCity = cityService.findCityByName(strs[1]);
						ticketVo.setStartCity(new CityVo(startCity));
						ticketVo.setEndCity(new CityVo(endCity));
					}
					return new AppResponse(ticketVo);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AppResponse(null);
	}
	
	@RequestMapping("addTicket")
	@ResponseBody
	public Object addTicket(Ticket ticket) {
		try {
			AppResponse appResponse = ticketService.addTicket(ticket);
			return appResponse;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AppResponse(Constant.code_error, Constant.error);
	}
}
